home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0051_OPTIMIZE.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  4KB  |  107 lines

  1. {
  2. BOB SWART
  3.  
  4. > Does anybody have any tips on optimizing TP Programs?
  5. What kind of optimization? Speed or Size? Optimizing For one may not be the
  6. same as optimizing For the other...
  7.  
  8. > but now it has grown quite large (anybody want it? :), and I'd like
  9. > to shrink it.
  10. Ah, so optimizing For size! Minimizing data space, code space (and stack/heap
  11. usage as well).
  12.  
  13. > I've gotten it from 40k down to 29k after a lot of work, but that is
  14. > still too big.
  15. Do you want to turn it into a TSR?
  16.  
  17. > Does anyone know of any common optimization techniques that would work?
  18. Do you use BAsm code or plain Pascal?
  19.  
  20. > For instance, if inc(IntVar, amt) is more efficient (code size wise)
  21. > than IntVar := IntVar + amt;
  22. Yes, try dumpprog (by our beloved moderator) on those two statements:
  23.  
  24. test.pas#4:  i := i + 4;
  25.    0000:000F A15000          MOV     AX,[DS: i(0050)]
  26.    0000:0012 050400          ADD     AX,0004
  27.    0000:0015 A35000          MOV     [DS: i(0050)],AX
  28.  
  29. It takes 9 Bytes For "i := i + 4;"
  30.  
  31. test.pas#5:  Inc(i,4);
  32.    0000:0018 8306500004      ADD     [Word DS:+i(+0050)],+04
  33.  
  34. It takes only 5 Bytes to do "Inc(i,4);" (and it is also faster!!)
  35.  
  36.  
  37. > That's the kind of thing that I'm looking for.
  38. Well Brian, currently I'm working on a whole BOOK about 'Borland Pascal
  39. Performance Optimization' (about 250-pages, english, early '94 ). In my book,
  40. the process op Program optimization is divided into four steps: 1. finding the
  41. bottle-necks in your Program, 2. using better datastructures & algorithms, 3.
  42. using more efficient language Constructs, and 4. using BAsm code and InLine
  43. macros. There will be a whole chapter devoted to 'optimization techniques for
  44. Program size', but I will say a few Words here For you:
  45.  
  46. Most of the times optimization is a matter of SPEED vs. SIZE. if you want the
  47. smallest code, then prepare let the Program do some more work. Eliminate big
  48. look-up tables (if you use any), use small, simple datastructures (that often
  49. imply not-so-efficient algorithms), do not use more Units than the ones you
  50. Absolutely need. Even then, try to code the routines from those Units yourself
  51. (avoid any and all overhead from those Units). If, For example, you need a
  52. ReadKey-like Function, don't use the Crt Unit, but implement your own ReadKey
  53. Function like this:
  54.  
  55. {$A-,B-,D-,E-,F-,G-,I-,L-,N-,O-,P-,Q-,R-,S+,T-,V-,X+}
  56. {.$DEFINE Crt}
  57. Program test;
  58. {$IFDEF Crt}
  59. { Code size: 3056
  60.   Data size:  690
  61.   .EXE size: 3232
  62. }
  63. Uses Crt;
  64. {$else}
  65. { Code size: 1504 --> 1552 Bytes less
  66.   Data size:  672 -->   18 Bytes less
  67.   .EXE size: 1680 --> 1552 Bytes less
  68. }
  69. Const
  70.   ScanCode : Byte =   0;
  71.   _ReadKey : Byte = $00;
  72.  
  73. Function ReadKey : Char; Assembler;
  74. Asm
  75.   mov   AL, ScanCode { check old ScanCode }
  76.   mov   ScanCode,0   { empty old ScanCode }
  77.   or    AL, AL       { AL = 0? }
  78.   jne   @end         { no: return ScanCode }
  79.   xor   AH, AH       { AH := 0 }
  80.   int   $16          { read Character }
  81.   or    AL, AL       { AL = 0? }
  82.   jne   @end         { no: simple Character }
  83.   mov   ScanCode, AH { yes: extended Character }
  84.  @end:
  85. end;
  86. {$endIF}
  87.  
  88. Var
  89.   t : Char;
  90. begin
  91.   t := ReadKey;
  92. end.
  93.  
  94. The resulting code is 1552 Bytes less when using your own ReadKey instead of
  95. the Crt Unit. This is mainly due to the initalization code of the Crt Unit, of
  96. course, but even For you 1.5 Kb is about 5% code size...
  97.  
  98. As you can see above, if you try to push your code to the limit, you MUST use
  99. BAsm or InLine macros. The Turbo/Borland Pascal compilers simply do not
  100. generate code as efficient as a good Programmer can do.
  101.  
  102. Finally, if you can't wait Until early '94, an article about Borland Pascal
  103. Performance Optimization will be published in an opcoming issue of PC
  104. Techniques. if you want more information about the book send me some netmail or
  105. Write to the address below. I'll send you some information on paper.
  106.  
  107.